/*cgq 2021 3 26 自定义指令 <div v-draggable>666</div>*/

/**
 * 自动指令
 * 2022 2 15
 * <div v-draggable>666</div>
 */
class instruction {
  __proto__ = "";
  // 拖拽
  draggable = {
    beforeMount(el: any) {
      el.style.cursor = "move";
      el.onmousedown = function (e: any) {
        let disx = e.pageX - el.offsetLeft;
        let disy = e.pageY - el.offsetTop;
        document.onmousemove = function (e) {
          let x = e.pageX - disx;
          let y = e.pageY - disy;
          let maxX =
            document.body.clientWidth -
            parseInt(window.getComputedStyle(el).width);
          let maxY =
            document.body.clientHeight -
            parseInt(window.getComputedStyle(el).height);
          if (x < 0) {
            x = 0;
          } else if (x > maxX) {
            x = maxX;
          }

          if (y < 0) {
            y = 0;
          } else if (y > maxY) {
            y = maxY;
          }
          el.style.position = "relative";
          el.style.left = x + "px";
          el.style.top = y + "px";
        };
        document.onmouseup = function () {
          document.onmousemove = document.onmouseup = null;
        };
      };
    },
  };

  permission = {
    mounted(el, binding, vNode) {
      const permission = binding.value; // 获取到 v-permission的值
      if (permission) {
        const hasPermission = checkArray(permission);
        if (!hasPermission) {
          // 没有权限 移除Dom元素
          el.parentNode && el.parentNode.removeChild(el);
        }
      }
    },
    // 当传进来的值更新的时候触发
    beforeUpdate(el, { value }) {
      el.$value = value;
    },
    // 指令与元素解绑的时候，移除事件绑定
    unmounted(el) {
      el.removeEventListener("click", el.handler);
    },
  };

  MU = {
    beforeMount(el, b) {
      let val = b.value;
      let fixexs = (val / 100000000).toFixed(2);
      if (fixexs == "NaN") {
        fixexs = "-";
      }
      el.innerHTML = fixexs + "亿";
    },
  };
  AU = {
    beforeMount(el, b) {
      let val = b.value;
      let fixexs = (val / 10000).toFixed(2);
      if (fixexs == "NaN") {
        fixexs = "-";
      }
      el.innerHTML = fixexs + "万";
    },
  };
  toFixed = {
    beforeMount(el, b) {
      let val = b.value.toFixed(2);
      el.innerHTML = val;
    },
  };
  setColour = {
    beforeMount(el, b) {
      let val = b.value;
      return el.classList.add(val ? "red" : "ls");
    },
  };
}
function checkArray(key) {
  if (!localStorage.rolemanagement) {
    return [];
  }
  const arr = localStorage.rolemanagement.split(",");
  const index = arr.indexOf(key);
  if (index > -1) {
    return true; // 有权限
  } else {
    return false; // 无权限
  }
}
export default instruction;

/*  // 指令绑定元素挂载前
beforeMount(el) {},
// 指令绑定元素挂载后
mounted(el, binding) {},
// 指令绑定元素因为数据修改触发修改前
beforeUpdate(el) {},
// 指令绑定元素因为数据修改触发修改后
updated(el) {},
// 指令绑定元素销毁前
beforeUnmount(el) {},
// 指令绑定元素销毁后
unmounted(el) {},*/
